import java.util.LinkedList;
import java.util.List;

public class BitSequence {
	private List<Boolean> lst;
	
	public BitSequence(){
		lst = new LinkedList<Boolean>();
	}
	
	public void append(boolean b){
		lst.add(b);
	}
	
	public void addLast(boolean b){
		lst.add( 0 , b );
	}
	
	public void addLast(char c){
		BitSequence bs = new BitSequence();
		byte b = (byte) c;
		int num = 0;
 		for (int i = 0; i < 8; i++ ) {
 			num = (int) Math.pow(2, i);
			bs.addLast( (num & b) > 0  );
		}
 		this.append(bs);
	}
	
	public void append(BitSequence bs){
		lst.addAll(bs.lst);
	}
	
	public boolean get(int index){
		return lst.get(index);
	}
	
	public int size(){
		return lst.size();
	}
	
	public boolean pollBit(){
		if(!lst.isEmpty())
			return lst.remove(0);
		else return false;
	}
	
	public char pollChar(){
		byte b = 0;
		if(lst.size() < 8) return (char) b;
        List<Boolean> lstBln = lst.subList(0, 8);
        for (int i = 7; i >= 0; i--) {
                if (lstBln.get(7-i)) 
                        b |= (byte)Math.pow(2, i) ; 
        }
        lst.subList(0, 8).clear();
        return (char)b; 
	}
	
	public boolean isEmpty()
	{
		return lst.isEmpty();
	}
	
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		for (boolean b : lst) {
			sb.append((b==true)?"1":"0");
		}
		return sb.toString();
	}
}
